package oct2013.linkednode;

import java.util.Stack;

public class SameTree {
	public boolean isSameTree(TreeNode p, TreeNode q) {
		Stack<TreeNode> stack = new Stack<TreeNode>();
		stack.push(q);
		stack.push(p);
		while (!stack.isEmpty()) {
			p = stack.pop();
			q = stack.pop();
			if (p == null && q == null)
				continue;
			else if (p == null || q == null)
				return false;
			else if (p.val != q.val)
				return false;
			else {
				stack.push(p.left);
				stack.push(q.left);
				stack.push(p.right);
				stack.push(q.right);
			}
		}
		return true;
	}
}
